﻿using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Security.Cryptography;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class KiemTraTheTinDung : System.Web.Services.WebService
{
    public KiemTraTheTinDung () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    SqlConnection Ket_Noi = new SqlConnection();
    private String Chuoi_CSDL = "CardSystem";
    private String Chuoi_vi_tri = ".\\SQLEXPRESS";
    private String Chuoi_nguoi_dung = "sa";
    private String Chuoi_mat_khau = "123";

    private void KetNoi()
    {
        String Chuoi_ket_noi = "";
        Chuoi_ket_noi = "Persist Security Info=False;Database=" + Chuoi_CSDL + ";Server=" + Chuoi_vi_tri + ";"
                            + "User=" + Chuoi_nguoi_dung + ";Password=" + Chuoi_mat_khau + ";Connect Timeout=30";
        Ket_Noi.ConnectionString = Chuoi_ket_noi;
        try
        {
            Ket_Noi.Open();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    [WebMethod(Description="Hàm kiểm tra thẻ có hợp lệ")]
    public bool KiemTraThe(string TenChuThe, byte[] MaThe, DateTime ThoiGianHetHan, String LoaiThe)
    {
        bool KetQua = true;
        SqlDataAdapter Bo_doc_ghi = new SqlDataAdapter();
        DataTable BangKetQua = new DataTable();
        KetNoi();
        SqlCommand command = new SqlCommand();        
        try
        {
            Bo_doc_ghi.SelectCommand = new SqlCommand("Select * from " + LoaiThe + " where Owner='" + TenChuThe
                                     + "' and Code=@MaThe"
                                     + " and ExpiredDate='" + ThoiGianHetHan.ToString() + "'",Ket_Noi);
            Bo_doc_ghi.SelectCommand.Parameters.Add("@MaThe", SqlDbType.VarBinary, 100);
            Bo_doc_ghi.SelectCommand.Parameters["@MaThe"].Value = MaThe;
            Bo_doc_ghi.Fill(BangKetQua);
        }
        catch (SqlException ex)
        {
            throw ex;
        }
        if (BangKetQua.Rows.Count == 0)
            KetQua = false;
        return KetQua;
    }
    
}
